package com.usooft.metaTalent.core.system.service.base;

import com.usooft.framework.core.query.Limit;
import com.usooft.framework.core.query.Page;
import com.usooft.metaTalent.core.system.entity.EmployeeRole;
import com.usooft.metaTalent.core.system.entity.Role;
import com.usooft.metaTalent.core.system.mapper.EmployeeRoleMapper;
import com.usooft.metaTalent.core.system.query.EmployeeRoleQuery;
import com.usooft.metaTalent.core.system.service.EmployeeRoleService;
import com.usooft.metaTalent.core.system.wrapper.EmployeeRoleWrapper;
import java.lang.Long;
import java.lang.Override;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.lang.NonNull;

public abstract class BaseEmployeeRoleService implements EmployeeRoleService {
    @Autowired
    private EmployeeRoleMapper employeeRoleMapper;

    @Override
    public EmployeeRole get(@NonNull Long id) {
        return find(id).orElseThrow();
    }

    @Override
    public Optional<EmployeeRole> find(@NonNull Long id) {
        return Optional.ofNullable(employeeRoleMapper.get(id));
    }

    @Override
    public List<EmployeeRoleWrapper> list(EmployeeRoleQuery query) {
        return employeeRoleMapper.list(query);
    }

    @Override
    public Page<EmployeeRoleWrapper> page(EmployeeRoleQuery query) {
        int rows = employeeRoleMapper.count(query.getFilter());
        return new Page<>(rows, rows == 0 ? new ArrayList<>() : employeeRoleMapper.list(query));
    }

    protected Optional<EmployeeRole> findOne(EmployeeRoleQuery.Filter filter,
            EmployeeRoleQuery.Sorts sorts) {
        List<EmployeeRoleWrapper> wrappers = list(EmployeeRoleQuery.builder()
                                .filter(filter)
                                .sorts(sorts)
                                .limit(Limit.rowCount(1))
                                .build());
        return wrappers.stream().findFirst().map(Function.identity());
    }

    protected Optional<EmployeeRole> findOne(EmployeeRoleQuery.Filter filter) {
        return findOne(filter, EmployeeRoleQuery.Sorts.builder().build());
    }

    @Override
    public int count(EmployeeRoleQuery.Filter filter) {
        return employeeRoleMapper.count(filter);
    }

    protected EmployeeRole internalAdd(Long employeeId, Role role) {
        EmployeeRole employeeRole = new EmployeeRole();
        employeeRole.setEmployeeId(employeeId);
        employeeRole.setRoleId(Optional.ofNullable(role).map(Role::getId).orElse(null));

        employeeRole.setCreatedDate(LocalDateTime.now());
        employeeRole.setVersion(0);
        employeeRoleMapper.add(employeeRole);
        return employeeRole;
    }

    protected void internalUpdate(@NonNull EmployeeRole employeeRole) {
        employeeRole.setLastModifiedDate(LocalDateTime.now());

        int rows = employeeRoleMapper.update(employeeRole);
        if (rows == 0) {
            throw new OptimisticLockingFailureException("更新失败。可能员工角色 (" + employeeRole.getId() + ") 的版本 (" + employeeRole.getVersion() + ") 已不是最新");
        }
        employeeRole.setVersion(employeeRole.getVersion() + 1);
    }

    protected void internalDelete(@NonNull EmployeeRole employeeRole) {
        int rows = employeeRoleMapper.delete(employeeRole);
        if (rows == 0) {
            throw new OptimisticLockingFailureException("删除失败。可能员工角色 (" + employeeRole.getId() + ") 的版本 (" + employeeRole.getVersion() + ") 已不是最新");
        }
    }
}
